<template>
    <div class="human-ai">
        <el-button @click="createInsideAlarmBox()">生成室内报警盒子</el-button>

    </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getMyProjectManager } from "@/getMyProjectManager";
import { ESUeViewer, ESUnrealActor } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
const myProjectManager = getMyProjectManager()


const createInsideAlarmBox = async () => {
    // 计算位置、旋转和缩放
    const { position, rotation, scale } = await computedTransform([
        {
            "x": 909.0,
            "y": -1100.0,
            "z": 193
        },
        {
            "x": 595.0,
            "y": -1143.0,
            "z": 193
        },
        {
            "x": 545.0,
            "y": -564.0,
            "z": 193
        },
        {
            "x": 866.0,
            "y": -564.0,
            "z": 193
        }
    ])
    const sceneObject = myProjectManager.createSceneObject(ESUnrealActor);
    if (!sceneObject) return
    sceneObject.lastActorStatusChanged.don((status) => {
        if (status != 'created') return
        // 设置颜色
        sceneObject.callFunction('ModifyColor', {
            R: 1,
            G: 0,
            B: 0,
            A: 1
        })
        // 设置尺寸
        sceneObject.callFunction('ModifySize', {
            X: scale[0],
            Y: scale[1],
            Z: scale[2],
        })
    })
    // 盒子模型路径
    sceneObject.actorClass = "/Script/Engine.Blueprint'/Game/HSKJ/Blueprints/BP_Box_Stroke_Senior.BP_Box_Stroke_Senior'";
    // 位置
    sceneObject.position = position;
    // 旋转
    sceneObject.rotation = rotation;

    console.log(sceneObject)

}

/**
 * 计算盒子的位置、旋转和缩放
 * @param points 
 */
const computedTransform = async (points: { x: number, y: number, z: number }[]) => {
    // 1. 计算中心点
    const centerX = points.reduce((sum, point) => sum + point.x, 0) / points.length as number;
    const centerY = points.reduce((sum, point) => sum + point.y, 0) / points.length as number;
    const centerZ = points.reduce((sum, point) => sum + point.z, 0) / points.length as number;
    const centerPosition = [centerX, centerY, centerZ];

    // 2. 计算长和宽
    const length = Math.sqrt(Math.pow(points[1].x - points[0].x, 2) + Math.pow(points[1].y - points[0].y, 2)) / 100;
    const width = Math.sqrt(Math.pow(points[2].x - points[1].x, 2) + Math.pow(points[2].y - points[1].y, 2)) / 100;

    // 3. 计算向量 D
    const D = {
        x: points[1].x - points[0].x,
        y: points[1].y - points[0].y
    };

    // 4. 计算向量 D 的模长 d
    const d = Math.sqrt(D.x * D.x + D.y * D.y);
    D.x /= d;
    D.y /= d;

    // 5. 计算向量 D 与Y轴正方向的夹角的余弦值
    const cosAngle = D.y;

    // 6. 计算夹角的弧度值
    let angleRad = Math.acos(cosAngle);

    // 7. 将弧度值转换为角度值
    let heading = angleRad * (180 / Math.PI);

    // 8. 根据 D.x 的值调整角度的正负
    if (D.x > 0) {
        heading = -heading;
    }

    console.log(heading);

    const viewer = myProjectManager.activeViewer as ESUeViewer;
    const res = await viewer.UEPositionToLonLatAlt([centerPosition[0], centerPosition[1], centerPosition[2]]);
    return {
        position: res as [number, number, number],
        rotation: [heading, 0, 0] as [number, number, number],
        scale: [width, length, 5] as [number, number, number]
    };
};




</script>
<style scoped></style>